<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Info_Theme extends Controller{

public function action_index()
{
}

public function action_list()
{
	$theme = ORM::factory('theme');
	if ($_GET)
	{
		$from = arr::get($_GET, 'from', -1);
		$count = arr::get($_GET, 'count', -1);
		$order_by = arr::get($_GET, 'order', -1);

		switch ($order_by)
		{
		case 0:
			$theme->order_by('create_time', 'DESC');
			break;
		case 1:
			$theme->order_by('create_time', 'ASC');
			break;
		case 2:
			$theme->order_by('download_count', 'DESC');
			break;
		default:
			break;
		}
	}
	$query_result = $theme->find_all();
	if ( isset($from) AND isset($count) AND $from!= -1 AND $count != -1)
	{
		$result = $this->create_theme_list(array_slice($query_result->as_array(), $from, $count));
		echo json_encode($result);
		return;
	}
	$result = $this->create_theme_list(array_slice($query_result->as_array(), 0, count($query_result->as_array())));
	echo json_encode($result);
}

public function action_download()
{
	$theme = ORM::factory('theme');
	if ($_GET)
	{
		$id = arr::get($_GET, 'id', -1);
		$theme->where('id', '=', $id)->find();
		if ($theme->loaded())
		{
			$theme->download_count++;
			$theme->save();
			$this->force_download('uploads/'.$theme->apk_url);
		}
	}
}

protected function force_download($file) {
    // 截取文件扩展名
    $ext = explode(".", $file);
    switch($ext[sizeof($ext)-1]) {
        case 'jar': $mime = "application/java-archive"; break;
        case 'zip': $mime = "application/zip"; break;
        case 'jpeg': $mime = "image/jpeg"; break;
        case 'jpg': $mime = "image/jpg"; break;
        case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
        case "gif": $mime = "image/gif"; break;
        case "png": $mime = "image/png"; break;
        case "pdf": $mime = "application/pdf"; break;
        case "txt": $mime = "text/plain"; break;
        case "doc": $mime = "application/msword"; break;
        case "ppt": $mime = "application/vnd.ms-powerpoint"; break;
        case "wbmp": $mime = "image/vnd.wap.wbmp"; break;
        case "wmlc": $mime = "application/vnd.wap.wmlc"; break;
        case "mp4s": $mime = "application/mp4"; break;
        case "ogg": $mime = "application/ogg"; break;
        case "pls": $mime = "application/pls+xml"; break;
        case "asf": $mime = "application/vnd.ms-asf"; break;
        case "swf": $mime = "application/x-shockwave-flash"; break;
        case "mp4": $mime = "video/mp4"; break;
        case "m4a": $mime = "audio/mp4"; break;
        case "m4p": $mime = "audio/mp4"; break;
        case "mp4a": $mime = "audio/mp4"; break;
        case "mp3": $mime = "audio/mpeg"; break;
        case "m3a": $mime = "audio/mpeg"; break;
        case "m2a": $mime = "audio/mpeg"; break;
        case "mp2a": $mime = "audio/mpeg"; break;
        case "mp2": $mime = "audio/mpeg"; break;
        case "mpga": $mime = "audio/mpeg"; break;
        case "wav": $mime = "audio/wav"; break;
        case "m3u": $mime = "audio/x-mpegurl"; break;
        case "bmp": $mime = "image/bmp"; break;
        case "ico": $mime = "image/x-icon"; break;
        case "3gp": $mime = "video/3gpp"; break;
        case "3g2": $mime = "video/3gpp2"; break;
        case "mp4v": $mime = "video/mp4"; break;
        case "mpg4": $mime = "video/mp4"; break;
        case "m2v": $mime = "video/mpeg"; break;
        case "m1v": $mime = "video/mpeg"; break;
        case "mpe": $mime = "video/mpeg"; break;
        case "mpeg": $mime = "video/mpeg"; break;
        case "mpg": $mime = "video/mpeg"; break;
        case "mov": $mime = "video/quicktime"; break;
        case "qt": $mime = "video/quicktime"; break;
        case "avi": $mime = "video/x-msvideo"; break;
        case "midi": $mime = "audio/midi"; break;
        case "mid": $mime = "audio/mid"; break;
        case "amr": $mime = "audio/amr"; break;
        default: $mime = "application/force-download";
    }
    header('Content-Description: File Transfer');
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
}


protected function create_theme_list($array)
{
	$object = array();
	foreach ( $array as $theme)
	{
		array_push($object,
			array(
				'id'			 => $theme->id,
				'name'			 => $theme->name,
				'title'			 => $theme->title,
				'price_rmb' 	 => $theme->price_rmb,
				'price_usd' 	 => $theme->price_usd,
				'score' 		 => $theme->score,
				'apk_url' 	 	 => url::base().'uploads/'.$theme->apk_url,
				'preview_url' 	 => url::base().'uploads/'.$theme->preview_url,
				'description' 	 => $theme->description,
				'download_count' => $theme->download_count,
				'create_time' 	 => $theme->create_time,
				'update_time' 	 => $theme->update_time,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}


}

